home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.12 Dec 96 / Flat File Databases / Invoice Example ƒ / Functional ƒ / PictureFuncs.c < prev   
Encoding:
Text File  |  1996-03-11  |  3.8 KB  |  133 lines  |  [TEXT/CWIE]

  1. // functions used by the modal dialog displaying a picture.
  2. /* this series of functions opens and closes a pre-made file.  In order to understand how 
  3. this code works, you should also review the code in picture creator.c, as that code
  4. creates the file that is manipulated here */
  5.  
  6. #include "BTreeDef.h"
  7. #include "BTreeProtos.h"
  8. #include "StandardFile.h"
  9.  
  10. // some macros
  11. #define kPicBlockSize        100L
  12. #define kPicSpareBlocks    10L
  13. #define kPicType            'IExP'
  14. #define kPicCreator        'IExA'
  15.  
  16. // used to store size and data for picture and text
  17. struct DataInfoType {
  18.     long PicSize;
  19.     long PicAddr;
  20.     long StrSize;
  21.     long StrAddr;
  22.     };
  23. typedef struct DataInfoType DataInfoType;
  24.  
  25.  
  26. // These are variables that will be used by B-Tree Helper 
  27. // Note that BTH allows multiple open files, but each file must
  28. //have its own file control handle
  29. TreeListHandle             gPictureTrees;
  30. FileControlHandle        gPictureFCH;
  31. long                    gPicBlockSize;
  32. long                    gPicSpareBlocks;
  33. StandardFileReply        gPicFileReply;
  34. SFTypeList                gPicTypeList;
  35. short                    gPicNumTypes;
  36. short                    gPicPermission;
  37. short                    PicError;
  38. long                    DataSize;
  39. FileAddr                DataAddr;
  40. DataInfoType            DataInfo;
  41. FileAddr                DataInfoAddr;
  42. DialogPtr                 gPictureDialog;
  43. Boolean                    fileIsOpen;
  44.  
  45. // prototypes
  46. void HandlePictureDialog(void);
  47. void OpenPictureFile(void);
  48. void ClosePictureFile(void);
  49.  
  50.  
  51. // if you don't understand this function, you shouldn't be reading this...
  52. void HandlePictureDialog(void){
  53. short        theItem;
  54. Boolean        theEnd = FALSE;
  55. fileIsOpen = FALSE;
  56. gPictureDialog = GetNewDialog(260,NULL,(WindowPtr)-1);
  57. do{
  58.     ModalDialog(NULL,&theItem);
  59.     switch (theItem) {
  60.     case 1:
  61.         OpenPictureFile();
  62.         break;
  63.     case 2:
  64.         if (fileIsOpen)
  65.             ClosePictureFile();
  66.         // need to reset the picture in the item list
  67.         // otherwise mac toolbox gets confused.
  68.         // don't ask me why.
  69.         ReleaseResource(GetResource('PICT', 128));
  70.         theEnd = TRUE;
  71.         break;
  72.     }//switch
  73. }//do statement
  74. while (!theEnd);
  75. DisposeDialog(gPictureDialog);
  76. }
  77.  
  78. // close the picture file
  79. void ClosePictureFile(void) {
  80.     short theErr = Close_BTreeFile(&gPictureFCH);
  81.     fileIsOpen = FALSE;
  82.     }//function
  83.     
  84. // open the picture file    
  85. void OpenPictureFile(void) {
  86.     short OpenFileErr = noErr;
  87.     short itemNo;
  88.     short itemType;
  89.     Handle item;
  90.     Str255 theString; 
  91.     Rect    box;
  92. // set up for standard file call
  93.     gPicTypeList[0]= kPicType;
  94.     StandardGetFile(NULL, 1, gPicTypeList, &gPicFileReply);
  95.     if (!gPicFileReply.sfGood){
  96.         //return if user cancels
  97.         OpenFileErr = userCanceledErr;
  98.         return;
  99.     }
  100.     //pass the gFileReply to our B-Tree routine
  101.     OpenFileErr = FSpOpen_BTreeFile(&gPicFileReply,fsCurPerm,&gPictureFCH);
  102.     if (OpenFileErr != noErr)
  103.         return;
  104.     // read in the data in three steps.  First read in the little struct that has
  105.     // the information about the other pieces of data.  Its address was tucked away
  106.     // in the .application1 field of the file control record when we made this file
  107.     OpenFileErr = Read_Data(gPictureFCH,16,(**gPictureFCH).application1,(Ptr)&DataInfo);
  108.     fileIsOpen = TRUE;
  109.     //read in the picture data first
  110.     itemNo = 3; //picture item
  111.     // retrieve the Picture item handle
  112.     GetDialogItem(gPictureDialog, itemNo, &itemType, &item, &box);
  113.     // set handle size to the size of the pict in the file
  114.     SetHandleSize(item,DataInfo.PicSize);
  115.     HLock(item);
  116.     // read in the picture
  117.     OpenFileErr = Read_Data(gPictureFCH,DataInfo.PicSize,DataInfo.PicAddr,*item);
  118.     HUnlock(item);
  119.     //update the dialog item list
  120.     SetDialogItem(gPictureDialog,itemNo, itemType, item, &box);
  121.     // now do the same for the text, in the exact same way
  122.     itemNo = 4;
  123.     GetDialogItem(gPictureDialog, itemNo, &itemType, &item, &box);
  124.     OpenFileErr = Read_Data(gPictureFCH, DataInfo.StrSize, DataInfo.StrAddr, (Ptr)(theString+1));
  125.     theString[0] = DataInfo.StrSize;
  126.     SetDialogItemText(item,theString);
  127.     SetDialogItem(gPictureDialog,itemNo,itemType,item,&box);
  128.     // update the dialog display
  129.     DrawDialog(gPictureDialog);
  130.     
  131.     
  132.     }//function
  133.